package com.apps.Im.Tencent.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.apps.Im.Tencent.aspect.TencentRestApi;
import com.apps.Im.Tencent.config.TencentImConfig;
import com.apps.Im.Tencent.enums.ApiTemplate;
import com.apps.Im.Tencent.enums.GroupType;
import com.apps.Im.Tencent.service.IEsImRestApiService;
import com.apps.Im.Tencent.service.TLSSigAPIv2Service;
import com.apps.sannotation.constraints.NoNull;
import com.apps.saspect.ValidMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Slf4j
@Service
public class IEsImRestApiServiceImpl implements IEsImRestApiService {

    @Autowired
    private TencentImConfig tencentConfig;

    @Autowired
    private RestTemplate restTemplate;

    //sign生成器
    @Autowired
    private TLSSigAPIv2Service tlsSigAPIv2Service;

    @Autowired
    private ApplicationContext applicationContext;

    //sign过期时间
    private Long sign_expire=2000L;

    /**
     *
     * @param apiTemplate
     * @return
     */
    @ValidMethod
    @Override
    public String RequestUrl(@NoNull(errMsg = "接口不能为空") ApiTemplate apiTemplate)
    {
        return tencentConfig.getIm_domain() + apiTemplate.getApi() + generParams(tencentConfig.getIm_adminAccount(),sign_expire);
    }

    /**
     * 生成用户传参
     * @param uname
     * @param expire
     * @return
     */
    private String generParams(String uname,Long expire)
    {
        return "?sdkappid="+tencentConfig.getIm_SDKAppID()+"&identifier="+uname+"&usersig="+tlsSigAPIv2Service.genSig(uname,expire)+"&random="+ RandomUtil.randomNumbers(10) +"&contenttype=json";
    }

    @TencentRestApi
    @ValidMethod
    @Override
    public JSONObject createBaseGroup(@NoNull(errMsg = "群名字不能为空") String group_name,
                                      @NoNull(errMsg = "群主账号不能为空") String master,
                                      @NoNull(errMsg = "群类型不能为空") GroupType groupType)
    {
        //先导入下用户,避免没导入去调接口出错
        applicationContext.getBean(this.getClass()).accountImport(master,"","");
        JSONObject jsons=new JSONObject();
        jsons.put("Owner_Account",master);
        jsons.put("Type",groupType.toString());
        jsons.put("Name",group_name);
        return restTemplate.postForObject(RequestUrl(ApiTemplate.create_group),jsons, JSONObject.class);
    }

    @TencentRestApi
    @ValidMethod
    @Override
    public JSONObject deleteGroup(@NoNull(errMsg = "群ID不能为空") String group_id)
    {
        JSONObject jsons=new JSONObject();
        jsons.put("GroupId",group_id);
        return restTemplate.postForObject(RequestUrl(ApiTemplate.destroy_group),new JSONObject(jsons), JSONObject.class);
    }

    @TencentRestApi
    @ValidMethod
    @Override
    public JSONObject accountImport(@NoNull(errMsg = "账号名不能为空") String uname, String nick_name, String face_url)
    {
        JSONObject jsons=new JSONObject();
        jsons.put("Identifier",uname);
        jsons.put("Nick",nick_name);
        jsons.put("FaceUrl",face_url);
        return restTemplate.postForObject(RequestUrl(ApiTemplate.account_import),jsons, JSONObject.class);
    }

    @TencentRestApi
    @ValidMethod
    @Override
    public JSONObject accountImport(@NoNull(errMsg = "账号列表不能为空") List<String> unames) {
        JSONObject jsons=new JSONObject();
        jsons.put("Accounts",unames);
        return restTemplate.postForObject(RequestUrl(ApiTemplate.multiaccount_import),new JSONObject(jsons), JSONObject.class);
    }

}
